//
//  HeartRateZoneView.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/2.
//

import Foundation
import MYModule
import UIKit

open class HeartRateZoneView: View {
    
    public var viewModel: HeartRateZoneModel = HeartRateZoneModel()
  
    open var hiddemDendescription: Bool = false {
        
        didSet {
            
            descriptionView.isHidden = hiddemDendescription
            
            analyseStackView.snp.remakeConstraints { make in
                make.top.equalTo(stateStackViewView.snp.bottom).offset(18)
                make.leading.equalTo(20)
                make.centerX.equalToSuperview()
                if self.hiddemDendescription {
                    make.bottom.equalToSuperview().offset(-20)
                }
            }
        }
    }
    
    
    open override func makeUI() {
        super.makeUI()
        
        self.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        let hourStack = StackView()
        hourStack.axis = .horizontal
        hourStack.alignment = .lastBaseline
        hourStack.contentMode = .center
        hourStack.addArrangedSubview(hourLabel)
        hourStack.addArrangedSubview(hourUntilLabel)
        
        let minuteStack = StackView()
        minuteStack.axis = .horizontal
        minuteStack.alignment = .lastBaseline
        minuteStack.contentMode = .center
        minuteStack.addArrangedSubview(minuteLabel)
        minuteStack.addArrangedSubview(minuteUntilLabel)
        
        let dataStack = StackView()
        dataStack.axis = .horizontal
        dataStack.alignment = .center
        dataStack.contentMode = .center
        
        dataStack.addArrangedSubview(hourStack)
        dataStack.addArrangedSubview(minuteStack)

        let labelStackView = StackView()
        labelStackView.axis = .vertical
        labelStackView.alignment = .center
        
        self.addSubview(labelStackView)
        labelStackView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        labelStackView.addArrangedSubview(dataStack)
        labelStackView.addArrangedSubview(totalTimeTitleLabel)

        self.addSubview(stateView)
        stateView.snp.makeConstraints { make in
            make.top.equalTo(labelStackView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
            make.height.equalTo(12)
        }
        
        stateView.addSubview(stateStackViewView)
        stateStackViewView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(analyseStackView)
        analyseStackView.snp.makeConstraints { make in
            make.top.equalTo(stateStackViewView.snp.bottom).offset(18)
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(self.descriptionView)
        self.descriptionView.snp.makeConstraints { make in
            make.top.equalTo(analyseStackView.snp.bottom)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20).priority(999)
        }
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        
        var i = 0
        
        let hour = self.viewModel.hour
        let minute = self.viewModel.minute
        let second = self.viewModel.second
        
        if hour == 0 && minute == 0 {
            
            if second == 0 {
                
                hourLabel.text = "--"
                minuteLabel.text = "--"
                
            } else {
                
                hourLabel.text = "0"
                minuteLabel.text = "1"
            }
            
        } else {
            
            hourLabel.text = "\(hour)"
            minuteLabel.text = "\(minute)"
            
        }
        
        analyseStackView.removeAllSubViews()
        stateStackViewView.removeAllSubViews()
        
        //心率区是否只有一种，如果只有一种，那么stackView的间隔就需要修改，item的宽度也需要修改，否则会出现最左或者最右不是圆角问题
        var isOnlyOne = false
        
        let totalSecond = self.viewModel.totalSecond
        
        self.viewModel.subModels.forEach { model in
           
            let time = model.second
            if totalSecond == time {
                isOnlyOne = true
            }
        }
        
        var noZeroCount = 0
        for model in self.viewModel.subModels {
            if model.second != 0 {
                noZeroCount += 1
            }
        }
        
        let totalWidth = noZeroCount > 0 ? stateStackViewView.bounds.size.width - CGFloat(noZeroCount - 1) : stateStackViewView.bounds.size.width
        log.debug("frame:\(stateStackViewView.frame.size.width)---bounds:\(stateStackViewView.bounds.size.width)")
        self.stateStackViewView.spacing = isOnlyOne == false ? 1 : 0
        
        
        
        self.viewModel.subModels.forEach { model in
            
            let iconColor = model.type.tintColor

            var hour: Int = 0
            var minute: Int = 0
            var second: Int = 0
            
            if !self.viewModel.isEmpty {
                
                let time = model.second
            
                hour = time/3600
                minute = (time - hour * 3600)/60
                second = time - hour * 3600 - minute * 60
                
                if time != 0 {

                    let itemWith = totalWidth/CGFloat(totalSecond) * CGFloat(time)
                    let itemView = View()
                    itemView.backgroundColor = iconColor
                    stateStackViewView.addArrangedSubview(itemView)
                    itemView.snp.makeConstraints { make in
                        make.width.equalTo(itemWith)
                    }
                }
                
            } else {
                
                
                let itemWith = totalWidth/5
                
                let itemView = View()
                itemView.backgroundColor = iconColor
                stateStackViewView.addArrangedSubview(itemView)
                itemView.snp.makeConstraints { make in
                    make.width.equalTo(itemWith)
                }
            }
            
            let view = HeartRateZoneItem()
            view.updateDate(iconColor: iconColor, title: model.type.title, hour: hour, minute: minute, second: second)
            analyseStackView.addArrangedSubview(view)
            i += 1
            
        }
        
        self.stateStackViewView.isHidden = viewModel.isEmpty
    }
    
    public func bind(to viewModel: HeartRateZoneModel) {
        
        self.viewModel = viewModel
        
        self.layoutIfNeeded()
    }
    
    
    //MARK: - lazy load -
    public lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 14)
        label.textColor = UIColor.assist_222222
        label.text = "heart_rate_zone_title".language
        return label
    }()
    
    public lazy var hourLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 44)
        label.text = "--"
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        return label
    }()
    
    public lazy var hourUntilLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 12)
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        label.text = "heart_rate_zone_hour".language
        return label
    }()
    
    public lazy var minuteLabel: Label = {
        let label = Label()
        label.text = "--"
        label.font = Fonts.boldSystemFont(ofSize: 44)
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        return label
    }()
    
    public lazy var minuteUntilLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 12)
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        label.text = "heart_rate_zone_min".language
        return label
    }()
    
    public lazy var totalTimeTitleLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 10)
        label.text = "heart_rate_zone_total_time_title".language
        label.textColor = UIColor.assist_222222
        label.textAlignment = .center
        return label
    }()
    
    private lazy var stateView: UIView = {
        let stateView = UIView()
        stateView.backgroundColor = UIColor.background
        stateView.layer.cornerRadius = 6
        stateView.layer.masksToBounds = true
        return stateView
    }()
    
    private lazy var stateStackViewView: StackView = {
        let stateStackViewView = StackView()
        stateStackViewView.axis = .horizontal
        stateStackViewView.spacing = 1
        stateStackViewView.alignment = .fill
        stateStackViewView.distribution = .fill
        return stateStackViewView
    }()
    
    private lazy var analyseStackView: StackView = {
        let analyseStackView = StackView()
        analyseStackView.axis = .vertical
        analyseStackView.spacing = 18
        analyseStackView.alignment = .fill
        analyseStackView.distribution = .fill
        return analyseStackView
    }()

    public lazy var descriptionView: ExPlainView = {
        let view = ExPlainView()
        return view
    }()
    
}
